<?php
namespace app\console\controller;

use app\console\model\MemberGrade as MemberGradeModel;
use think\Request;
use think\Url;
use think\Db;
use think\Controller;

class MemberGrade extends Controller
{
    /*
     * 会员等级列表
     */
    public function index()
    {

        if (Request::instance()->isPost()) {

            if (Request::instance()->param('name')) {

                // 提交数据
                (new MemberGradeModel())->allowField(true)->save(Request::instance()->param());

            }

            $ids = Request::instance()->param('ids/a');
            $name = Request::instance()->param('MemberName/a');
            $expA = Request::instance()->param('expA/a');
            $expB = Request::instance()->param('expB/a');
            $picArr = Request::instance()->param('picArr/a');

            for ($i = 0; $i < count($ids); $i++) {
                MemberGradeModel::where('id', $ids[$i])->update(['name' => $name[$i], 'exp_a' => $expA[$i], 'exp_b' => $expB[$i], 'picUrl' => $picArr[$i]]);
            }

        }

        $data = MemberGradeModel::order('id', 'asc')->select();

        return $this->fetch('index', [
            "mate_title" => '会员等级列表',
            'data'       => $data
        ]);

    }

    /*
     * 会员等级删除
     */
    public function destroy($id)
    {

        if (MemberGradeModel::destroy($id)) {
            win('删除成功', Url::build('console/member_grade/index'));
        }

        fail('删除失败');

    }
}
